package spittr.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import spittr.Spitter;
import spittr.data.SpitterRepository;

@Controller
@RequestMapping("/spitter")
public class SpitterController {

	private SpitterRepository mockRegisitory;
	
	SpitterController(){
		this(null);
	}
	
	@Autowired
	SpitterController(SpitterRepository mockRegisitory){
		this.mockRegisitory=mockRegisitory;
	}
	
	@RequestMapping(value="/register", method=RequestMethod.GET)
	public String showRegistrationForm(){
		return "registerForm";
	}
	
	@RequestMapping(value="/register", method=RequestMethod.POST)
	public String processRegistation(Spitter spitter){
		mockRegisitory.save(spitter);
		return "redirect:/spitter/"+spitter.getUserName();//进行重定向，调用showSpitterProfile方法。
	}
	
	@RequestMapping(value="/{username}",method=RequestMethod.GET)
	public String showSpitterProfile(@PathVariable String username,Model model){
		Spitter spitter=mockRegisitory.findByUsername(username);
		model.addAttribute(spitter);
		return "profile";
	}
}
